package com.huawei.codearts.controller;

import com.huawei.codearts.service.OrderService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * JDBCTemplate 模式 增删改查
 */

//@Slf4j  // 如果开启 则 LoggerFactory.getLogger(OrderController.class) 需要关闭
@RestController
@RequestMapping(value = "order")
public class OrderController {

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @RequestMapping(value = "getOne")
    public String getOne(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("id"));
        int id = Integer.parseInt((String)params.get("id"));

        String result = ResponseUtils.response();
        try {
            result = ResponseUtils.response(orderService.getOne(id));
        } catch(Exception e) {
            // 处理 Class.forName 错误
//            e.printStackTrace();
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "getList")
    public String getList(@RequestParam Map<String, Object> params) {
        String result = ResponseUtils.response();
        try {
            result = ResponseUtils.response(orderService.getList());
        } catch(Exception e) {
            // 处理 Class.forName 错误
//            e.printStackTrace();
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "add")
    public String insert(@RequestParam Map<String, Object> params) {
        String result = ResponseUtils.error("创建失败");
        try {
            Integer res = orderService.insert();
            if (res > 0) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
//            e.printStackTrace();
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "update")
    public String update(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        String result = ResponseUtils.error("更新失败");
        try {
            Integer res = orderService.update(id);
            if (res > 0) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
//            e.printStackTrace();
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "del")
    public String del(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        String result = ResponseUtils.error("删除失败");
        try {
            Integer res = orderService.del(id);
            if (res > 0) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
//            e.printStackTrace();
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "updateWithTx")
    public String updateWithTx(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        int id = Integer.parseInt((String)params.get("id"));
        String result = ResponseUtils.error("更新失败");
        try {
            boolean res = orderService.updateWithTx(id);
            if (res) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "springWithTx")
    public String springWithTx(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        int id = Integer.parseInt((String)params.get("id"));
        String result = ResponseUtils.error("更新失败");
        try {
            boolean res = orderService.springWithTx(id);
            if (res) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

}
